package com.house.business.service.impl;

import com.house.business.entity.Notice;
import com.house.business.vo.NoticeVo;
import com.house.business.mapper.NoticeMapper;
import com.house.business.service.NoticeService;
import com.house.framework.entity.AjaxResult;
import com.house.framework.service.impl.ServiceBaseImpl;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import com.house.framework.annotations.Slave;

import java.util.Date;

/**
 * <p>
 * 公告表 服务实现类
 * </p>
 *
 * @since 2023-05-09
 */
@Service
public class NoticeServiceImpl extends ServiceBaseImpl<NoticeMapper, Notice> implements NoticeService {

    @Autowired
    private NoticeMapper noticeMapper;

    /**
    * 新增公告表
    * @param entity
    * t_notice新增数据
    * @return
    */
    @SneakyThrows
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Object addNotice(Notice entity){
        entity.setReleaseTime(new Date());
        noticeMapper.insert(entity);
        return AjaxResult.success();
    }

    /**
    * 修改公告表
    * @param entity
    * t_notice修改数据
    * @return
    */
    @SneakyThrows
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Object updateNotice(Notice entity){
        noticeMapper.updateById(entity);
        return AjaxResult.success();
    }

    /**
    * 取指定ID的公告表数据
    * @param id
    * t_notice 主键ID
    * @return NoticeVo
    * 返回指定ID的公告表数据
    */
    @Slave
    @Override
    public NoticeVo getNoticeById(Long id) {
        return noticeMapper.get(id);
    }

    /**
    * 公告表分页取数
    * @param entityVo
    * t_notice 分页取数参数，pageIndex=页码；pageSize=每页记录数；
    * @return
    * 返回分页数据
    */
    @Slave
    @Override
    public IPage<NoticeVo> getNoticeList(NoticeVo entityVo) {
        int pageIndex = entityVo.getPageIndex();
        int pageSize = entityVo.getPageSize();
        IPage page = new Page<NoticeVo>(pageIndex, pageSize);
        QueryWrapper<NoticeVo> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .like(StringUtils.isNotEmpty(entityVo.getUserName()), "user_name", entityVo.getUserName())
                .like(StringUtils.isNotEmpty(entityVo.getKeyWords()), "content", entityVo.getKeyWords())
                .orderByDesc("release_time");
        return noticeMapper.page(page, queryWrapper);
    }

    /**
    * 删除指定ID的公告表数据
    * @param id
    * t_notice 主键ID
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteNoticeById(Long id){
        noticeMapper.delete(id);
    }

    /**
    * 逻辑删除指定ID的公告表数据
    * @param id
    * t_notice 主键ID
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void logicDeleteById(Long id){
        this.logicDelete(id, NoticeMapper.class, Notice.class);
    }


    /**
    * 批量删除指定ID的公告表数据
    * @param ids
    * t_notice 主键ID数组
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void batchDelete(Long[] ids){
        noticeMapper.batchDelete(ids);
    }

    /**
    * 批量逻辑删除指定ID的公告表数据
    * @param ids
    * t_notice 主键ID数组
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void batchLogicDelete(Long[] ids){
        for(Long id : ids){
            this.logicDelete(id, NoticeMapper.class, Notice.class);
        }
    }
}
